<?php

//搜索控制器
class Search extends Controller {

	var $userid;
	
	function Search()
	{
		parent::Controller();	
		$this->load->database();
		$this->load->model("accounts_model", "am");
		$this->load->model("search_model");
		
		//登陆状态检测
		$this->userid = $this->am->is_logined();
		$this->account = $this->am->get_account();
	}
	
	//搜索默认首页
	function index()
	{
		//输出公共header
		$pagedata["topmenuid"] = 5;
		if(!isset($pagedata["title"])) $pagedata["title"] = "搜索 | 热闹网";
		if(!isset($pagedata["keywords"])) $pagedata["keywords"] = "热闹、热闹网、热闹100、renao、renao100、轻网站、网站、小站、微网站、博客、社区、互动";
		if(!isset($pagedata["description"])) $pagedata["description"] = "热闹网(www.renao100.com) - 前所未有的互动轻网站社区。轻网站是一个创意点子的实现，每个人拥有属于自己的轻网站，并且允许共同维基创建内容。适合个人站、兴趣站、才艺站、组织站甚至官方站。轻网站可以是WEB1.0，更可以是WEB2.0。";
		$this->autoload_model->output_header($pagedata);
		$result = array();
		
		//参数处理
		parse_str($_SERVER["QUERY_STRING"], $this->param);
		if(!isset($this->param["keyword"]) || $this->param["keyword"] == "") show_failed("非法请求");
		
		//搜索处理
		$keyword = safeEncoding(trim($this->param["keyword"]));//字符转换处理
		$action = $this->param["action"];
		if(!$action || $action == "") $action = "topic";
		switch ($action) {
			case "site"://热站搜索
				$this->site($keyword, $action);	
			break;
			case "user"://用户搜索
				$this->user($keyword, $action);
			break;
			default://话题搜索(模糊搜索+高亮显示)
				$this->topic($keyword, $action);
			break;
		}
		
		//输出公共footer
		$this->autoload_model->output_footer();
	}
	
	//热站搜索
	function site($keyword, $action)
	{
		//参数处理
		(!isset($this->param["page"]) || $this->param["page"] == "") ? $offset = 0 : $offset = $this->param["page"];
		$per_page = 20;
		
		$result = $this->search_model->site($keyword, $per_page, $offset);
		//分页处理
		$base_url = gurl('search/').'/?keyword='.$keyword.'&action='.$action;
		$total_rows = $this->search_model->counts($keyword, 'site');
		$pagestring = pages($base_url,$total_rows,3,$per_page, true);//query string
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		$viewdata["keyword"] = $keyword;
		$viewdata["result"] = $result;
		$this->load->view(__CLASS__.'/site', $viewdata);
	}
	
	//话题搜索
	function topic($keyword, $action)
	{
		//参数处理
		(!isset($this->param["page"]) || $this->param["page"] == "") ? $offset = 0 : $offset = $this->param["page"];
		$per_page = 40;
		
		$result = $this->search_model->topic($keyword, $per_page, $offset);
		//分页处理
		$base_url = gurl('search/').'/?keyword='.$keyword.'&action='.$action;
		$total_rows = $this->search_model->counts($keyword, 'topic');
		$pagestring = pages($base_url,$total_rows,3,$per_page, true);//query string
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		$viewdata["keyword"] = $keyword;
		$viewdata["result"] = $result;
		$this->load->view(__CLASS__.'/index', $viewdata);
	}
	
	//用户搜索
	function user($keyword, $action)
	{
		//参数处理
		(!isset($this->param["page"]) || $this->param["page"] == "") ? $offset = 0 : $offset = $this->param["page"];
		$per_page = 80;
		
		$result = $this->search_model->user($keyword, $per_page, $offset);
		//分页处理
		$base_url = gurl('search/').'/?keyword='.$keyword.'&action='.$action;
		$total_rows = $this->search_model->counts($keyword, 'user');
		$pagestring = pages($base_url,$total_rows,3,$per_page, true);//query string
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		$viewdata["keyword"] = $keyword;
		$viewdata["result"] = $result;
		$this->load->view(__CLASS__.'/user', $viewdata);
	}
}

/* End of file */